package com.et.u1.util.widget;

import android.annotation.SuppressLint;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
import java.util.concurrent.atomic.AtomicInteger;

public class PathAnimator extends AbstractPathAnimator {
    private final AtomicInteger mCounter = new AtomicInteger(0);
    private final Handler mHandler = new Handler(Looper.getMainLooper());

    public PathAnimator(Config config) {
        super(config);
    }

    public void start(final View child, final ViewGroup parent) {
        parent.addView(child, new LayoutParams(this.mConfig.heartWidth, this.mConfig.heartHeight));
        PathAnimator.FloatAnimation anim = new PathAnimator.FloatAnimation(this.createPath(this.mCounter, parent, 2), this.randomRotation(), parent, child);
        anim.setDuration(this.mConfig.animDuration);
        anim.setInterpolator(new LinearInterpolator());
        anim.setAnimationListener(new AnimationListener() {
            public void onAnimationEnd(Animation animation) {
                PathAnimator.this.mHandler.post(() -> parent.removeView(child));
                PathAnimator.this.mCounter.decrementAndGet();
            }

            public void onAnimationRepeat(Animation animation) {
            }

            public void onAnimationStart(Animation animation) {
                PathAnimator.this.mCounter.incrementAndGet();
            }
        });
        anim.setInterpolator(new LinearInterpolator());
        child.startAnimation(anim);
    }

    private static float scale(double a, double b, double c, double d, double e) {
        return (float) ((a - b) / (c - b) * (e - d) + d);
    }

    static class FloatAnimation extends Animation {
        private PathMeasure mPm;
        private View mView;
        private float mDistance;
        private float mRotation;

        @SuppressLint("WrongConstant")
        public FloatAnimation(Path path, float rotation, View parent, View child) {
            this.mPm = new PathMeasure(path, false);
            this.mDistance = this.mPm.getLength();
            this.mView = child;
            this.mRotation = rotation;
            parent.setLayerType(2, null);
        }

        protected void applyTransformation(float factor, Transformation transformation) {
            Matrix matrix = transformation.getMatrix();
            this.mPm.getMatrix(this.mDistance * factor, matrix, 1);
            this.mView.setRotation(this.mRotation * factor);
            float scale = 1.0F;
            if (3000.0F * factor < 200.0F) {
                scale = PathAnimator.scale(factor, 0.0D, 0.06666667014360428D, 0.20000000298023224D, 1.100000023841858D);
            } else if (3000.0F * factor < 300.0F) {
                scale = PathAnimator.scale(factor, 0.06666667014360428D, 0.10000000149011612D, 1.100000023841858D, 1.0D);
            }

            this.mView.setScaleX(scale);
            this.mView.setScaleY(scale);
            transformation.setAlpha(1.0F - factor);
        }
    }
}
